package com.dense.kuiniu.shelfcard.card.idcard;

import java.util.concurrent.TimeUnit;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.functions.Function;

/**
 * @author Administrator
 * @date 2020/8/31
 */
public class RetryWithDelay implements
        Function<Observable<Throwable>, ObservableSource<Object>> {
    int current = -1;
    int[] timeDely = new int[]{200, 200, 200, 200, 200, 200, 200, 200,
            200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200,};


    @Override
    public ObservableSource<Object> apply(Observable<Throwable> throwableObservable) throws Throwable {
        return throwableObservable.flatMap(new Function<Throwable, Observable<Long>>() {
            @Override
            public Observable<Long> apply(Throwable throwable) throws Exception {
                ++current;
                if (current < timeDely.length) {
                    return Observable.timer(timeDely[current], TimeUnit.MILLISECONDS);
                } else {
                    return Observable.error(throwable);
                }
            }
        });
    }

}